package zq.cn.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import zq.cn.activity.MainActivity;
import zq.cn.activity.R;
import zq.cn.activity.ViewInfoActivity;
import zq.cn.activity.SearchActivity;

/**
 * Created by ZQ on 2016/4/16.
 */
public class BtnTools extends AppCompatActivity {
    private Button setting;
    private PopupWindow pw;
    private Class[] classes = new Class[] {MainActivity.class, ViewInfoActivity.class, SearchActivity.class};
    private Button[] btn = new Button[3];
    private int[] btnColor0 = new int[] {R.drawable.botton_icon01b,R.drawable.botton_icon02b,R.drawable.botton_icon03b};
    private int[] btnColor1 = new int[] {R.drawable.botton_icon01a,R.drawable.botton_icon02a,R.drawable.botton_icon03a};
    private int i;
    private List<Activity> act = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        act.add(this);
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onDestroy() {
        act.remove(this);
        super.onDestroy();
    }

    protected void init(int index){

        btn[0] = (Button) findViewById(R.id.btn_0);
        btn[1] = (Button) findViewById(R.id.btn_1);
        btn[2] = (Button) findViewById(R.id.btn_2);
        for(i=0;i<btn.length;i++) {
            final int temp =i;
            if(i==index) {
                btn[i].setBackgroundResource(btnColor1[i]);
            }else {
                btn[i].setBackgroundResource(btnColor0[i]);
                btn[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent in = new Intent(BtnTools.this,classes[temp]);
                        startActivity(in);
                    }
                });
            }
        }

        setting = (Button) findViewById(R.id.btn_setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pw = new PopupWindow(Globals.SCREEN_WIDTH/4,Globals.SCREEN_HEIGHT/6);
                View view = LayoutInflater.from(BtnTools.this).inflate(R.layout.setting_info,null);
                pw.setContentView(view);
                if(pw.isShowing()) {
                    pw.dismiss();
                }else {
                    pw.showAsDropDown(setting);
                }
//              内部类调用
                TextView version = (TextView)view.findViewById(R.id.win_version);
                TextView about = (TextView)view.findViewById(R.id.win_about);
                TextView exit = (TextView)view.findViewById(R.id.set_exit);

                version.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw.dismiss();
                        Toast.makeText(BtnTools.this,"版本信息：1.0",Toast.LENGTH_LONG).show();
                    }
                });

                about.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw.dismiss();
                        AlertDialog.Builder builder = new AlertDialog.Builder(BtnTools.this);
                        builder.setTitle("关于我们");
                        builder.setMessage("张强 2016-04-19\r\n" + "导师：孙逊");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                        builder.create().show();
                    }
                });

                exit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pw.dismiss();
                        for(int i=0;i<act.size();i++) {
                            act.get(i).finish();
                        }
                    }
                });
            }
        });
    }
}
